package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsGroupon;
import com.qf.pojo.DtsGrouponRules;
import com.qf.service.DtsGoodsService;
import com.qf.service.DtsGrouponRulesService;
import com.qf.service.DtsGrouponService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zwnan
 */
@RestController
@RequestMapping("/admin/groupon")
@CrossOrigin
public class GrouponController {

    @Autowired
    private DtsGrouponRulesService dtsGrouponRulesService;

    @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "查找")
    @RequiresPermissions("admin:groupon:list")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1")Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc") String order){
        IPage<DtsGrouponRules> iPage = dtsGrouponRulesService.selectPage(page,limit,sort,order);
        long total = iPage.getTotal();
        List<DtsGrouponRules> records = iPage.getRecords();
        Map map = new HashMap();
        map.put("total", total);
        map.put("items",records);
        return ResponseUtil.ok(map);
    }

    @Autowired
    private DtsGoodsService dtsGoodsService;

    @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "添加")
    @RequiresPermissions("admin:groupon:create")
    @PostMapping("/create")
    public Object create(@RequestBody DtsGrouponRules dtsGrouponRules){
        Long goodsId = dtsGrouponRules.getGoodsId();
        String name = dtsGoodsService.selectByGoodsId(goodsId);
        String picUrl = dtsGoodsService.selectByGoodsId2(goodsId);
        dtsGrouponRules.setPicUrl(picUrl);
        dtsGrouponRules.setGoodsName(name);
        dtsGrouponRules.setAddTime(new Date());
        dtsGrouponRulesService.insert(dtsGrouponRules);


        return ResponseUtil.ok(dtsGrouponRules);
    }

    @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "编辑")
    @RequiresPermissions("admin:groupon:update")
    @PostMapping("/update")
    public Object update(@RequestBody DtsGrouponRules dtsGrouponRules){
        dtsGrouponRulesService.update(dtsGrouponRules);
        return ResponseUtil.ok();
    }
    @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "删除")
    @RequiresPermissions("admin:groupon:delete")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGrouponRules dtsGrouponRules){
        dtsGrouponRulesService.delete(dtsGrouponRules);
        return ResponseUtil.ok();
    }
    @GetMapping("/listRecord")
    public Object listRecord(@RequestParam(defaultValue = "1")Integer page,
                             @RequestParam(defaultValue = "20") Integer limit,
                             @RequestParam(defaultValue = "add_time") String sort,
                             @RequestParam(defaultValue = "desc") String order){

        IPage<DtsGrouponRules> iPage = dtsGrouponRulesService.selectPage(page,limit,sort,order);
        long total = iPage.getTotal();
        List<DtsGrouponRules> records = iPage.getRecords();
        Map map= new HashMap();
        map.put("total",total);
        map.put("items",records);
        return ResponseUtil.ok(map);

    }
}
